package com.ql.gamelogm.dao.impl;

import com.ql.gamelogm.dao.LogDao;
import com.ql.gamelogm.entities.Log;
import com.ql.gamelogm.entities.SearchHistory;
import com.ql.gamelogm.entities.WarnLog;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.stereotype.Repository;

import java.util.Date;
import java.util.List;
@Repository
public class LogDaoImpl implements LogDao {
    @Autowired
    private JdbcTemplate jdbcTemplate;
    @Override
    public void savelog(Log log) {
        String sql="insert into operationlog(date,operator,operation,ipaddr) values(?,?,?,?)";
        jdbcTemplate.update(sql,log.getDate(),
                                log.getOperator(),
                                log.getOperation(),
                                log.getIpaddr()
        );
    }

    @Override
    public List<Log> findAllLogs() {
        String sql="select * from operationlog ";
        List<Log> logList=jdbcTemplate.query(sql,(resultSet, rowNum) -> new Log(
                resultSet.getTimestamp("date"),
                resultSet.getString("operator"),
                resultSet.getString("operation"),
                resultSet.getString("ipaddr")
        ));
        return logList;
    }



    @Override
    public void SaveSearchHistory(SearchHistory searchHistory) {
        String sql="insert into searchhistory(operator,depository,searchtime,sentence,result,time) values(?,?,?,?,?,?)";
        jdbcTemplate.update(sql,searchHistory.getOperator(),
                                searchHistory.getDepository(),
                                searchHistory.getSearchtime(),
                                searchHistory.getSentence(),
                                searchHistory.getResult(),
                                searchHistory.getTime()
        );
    }

    @Override
    public List<SearchHistory> findAllSearchHistory() {
        String sql="select * from searchhistory ";
        List<SearchHistory> searchHistoryList=jdbcTemplate.query(sql,(resultSet, rowNum) -> new SearchHistory(
                resultSet.getInt("id"),
                resultSet.getString("operator"),
                resultSet.getString("depository"),
                resultSet.getTimestamp("searchtime"),
                resultSet.getString("sentence"),
                resultSet.getString("result"),
                resultSet.getString("time")
        ));
        return searchHistoryList;
    }


    @Override
    public List<WarnLog> findAllWarnLog() {
        String sql="select * from warnlog";
        List<WarnLog> warnLogList=jdbcTemplate.query(sql,(resultSet, rowNum) -> new WarnLog(
                resultSet.getInt("id"),
                resultSet.getString("state"),
                resultSet.getString("deg"),
                resultSet.getString("descr"),
                resultSet.getString("gamername"),
                resultSet.getFloat("avgrecovery"),
                resultSet.getFloat("avgresponse"),
                resultSet.getString("solveperson"),
                resultSet.getTimestamp("date"),
                resultSet.getTimestamp("dateres")
        ));
        return warnLogList;
    }

    @Override
    public List<WarnLog> findWarnLogById(int id) {
        String sql="select * from warnlog where id="+id;
        List<WarnLog> warnLogList=jdbcTemplate.query(sql,(resultSet, rowNum) -> new WarnLog(
                resultSet.getInt("id"),
                resultSet.getString("state"),
                resultSet.getString("deg"),
                resultSet.getString("descr"),
                resultSet.getString("gamername"),
                resultSet.getFloat("avgrecovery"),
                resultSet.getFloat("avgresponse"),
                resultSet.getString("solveperson"),
                resultSet.getTimestamp("date"),
                resultSet.getTimestamp("dateres")
        ));
        return warnLogList;
    }

    @Override
    public boolean saveWarnLog(WarnLog warnLog) {
        String sql="insert into warnlog(state,deg,descr,gamername,avgrecovery,avgresponse,solveperson,date,dateres) values(?,?,?,?,?,?,?,?,?)";
        int update = jdbcTemplate.update(sql,
                warnLog.getState(),
                warnLog.getDeg(),
                warnLog.getDescr(),
                warnLog.getGamername(),
                warnLog.getAvgrecovery(),
                warnLog.getAvgresponse(),
                warnLog.getSolveperson(),
                warnLog.getDate(),
                warnLog.getDateres()
        );
        if(update==0){
            return false;
        }
        return true;
    }

    @Override
    public boolean updateWaitWarnLogState(int id, WarnLog warnLog) {
        String sql="update warnlog set state=?,avgresponse=?,solveperson=?,date=?,dateres=? where id="+id;
        int update = jdbcTemplate.update(sql,
                warnLog.getState(),
                warnLog.getAvgresponse(),
                warnLog.getSolveperson(),
                warnLog.getDate(),
                warnLog.getDateres()
        );
        if(update==0){
            return false;
        }
        return true;
    }

    @Override
    public boolean updateAcepWarnLogState(int id, WarnLog warnLog) {
        String sql="update warnlog set state=?,avgrecovery=?,solveperson=?,date=?,dateres=? where id="+id;
        int update = jdbcTemplate.update(sql,
                warnLog.getState(),
                warnLog.getAvgrecovery(),
                warnLog.getSolveperson(),
                warnLog.getDate(),
                warnLog.getDateres()
        );
        if(update==0){
            return false;
        }
        return true;
    }

    @Override
    public boolean saveRecentTime(String date) {
        String sql="update analysis set recent=?";
        int update = jdbcTemplate.update(sql, date);
        if(update==0){
            return false;
        }
        return true;
    }
}
